package com.cy.pj.goods.controller;

import com.cy.pj.goods.pojo.Goods;
import com.cy.pj.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/goods/")//定义url中的共性问题
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @PostMapping("doUpdateGoods")
    public String doUpdateGoods(Goods goods,Model model){
        goodsService.updateGoods(goods);
       /* List<Goods> list = goodsService.findGoods(null);
        model.addAttribute("list",list);
        return "goods";*/
        return "redirect:/goods/doFindGoods";
        // return "forward:doFindGoods";//不行
    }

    @GetMapping("doFindById/{id}")
    public  String doFindById(@PathVariable Integer id, Model model){
        Goods goods=goodsService.findById(id);
        model.addAttribute("goods",goods);
        return "goods-update";
    }

    //@RequestMapping ("doSaveGoods")
    //@RequestMapping(value = "doSaveGoods",method = RequestMethod.GET)
    @RequestMapping ("doSaveGoods")
    public String doSaveGoods(Goods goods,Model model){
        goodsService.saveGoods(goods);
       return "redirect:/goods/doFindGoods";
       //return "forward:doFindGoods";//不行
        /*List<Goods> list = goodsService.findGoods(null);
        model.addAttribute("list",list);
        return "goods";*/
    }

    @RequestMapping("doGoodsAddUI")
    public String doGoodsAddUI(){
        return "goods_add";
    }

    @RequestMapping ("doDeleteById")
    public String doDeleteById(Integer id, Model model){
        goodsService.deleteById(id);
        /*List<Goods> list = goodsService.findGoods(null);
        list.forEach(System.out::println);
        model.addAttribute("list",list);
        return "goods";*/
       //return "forward:doFindGoods";
        return "redirect:/goods/doFindGoods";
    }



    @GetMapping("doFindGoods")
    public String doFindGoods(Model model,String name){
        List<Goods> list = goodsService.findGoods(name);
        model.addAttribute("list",list);
        return "goods";
    }




}
